package com.secsbrain.cmdb.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.IDeployEngineService;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.cmdb.client.model.App;
import com.secsbrain.cmdb.client.model.DeployEngineDto;
import com.secsbrain.cmdb.dal.model.DeployEngine;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.common.utils.RedisUtil;
import com.secsbrain.sso.core.SsoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 发布引擎控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/deployEngine")
public class DeployEngineController {
	
    @Autowired
    IDeployEngineService deployEngineService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult queryList(Integer page, Integer limit, DeployEngine params) {
    	Page<DeployEngine> pageBean=new Page<>(page, limit);
    	Page<DeployEngine> result=deployEngineService.selectPage(pageBean, params);
        return new ResponseResult<>(result);
    }
    
    /**
     * 功能描述: <br>
     * 查询所有列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    public ResponseResult<?> queryAllList(DeployEngine params) {
    	List<DeployEngine> list=deployEngineService.selectList(params);
    	return new ResponseResult<>(list);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody DeployEngine appEnv) {
    	appEnv.setCreateTime(new Date());
    	appEnv.setCreator(SsoHelper.currentUser().getNickname());
        deployEngineService.insertSelective(appEnv);
    	return new ResponseResult<>();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody DeployEngine appEnv) {
    	appEnv.setUpdateTime(new Date());
    	appEnv.setUpdator(SsoHelper.currentUser().getNickname());
        deployEngineService.updateSelectiveById(appEnv);

        RedisUtil.remove(DeployEngineDto.APP_REDIS_PREX+appEnv.getEnvName());
    	return new ResponseResult<>();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody DeployEngine appEnv) {
        deployEngineService.deleteById(appEnv.getId());
        RedisUtil.remove(DeployEngineDto.APP_REDIS_PREX+appEnv.getEnvName());
    	return new ResponseResult<>();
    }
    
}
